<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="筛选类型" prop="type"
                :rules="[
                 ]">
                 <el-select v-model="inputForm.type" placeholder="请选择户型类别" size="small" style="width: 100%;">
                  <el-option
                    v-for="item in valueList" v-if="item.type==16"
                    :key="item.valueNum"
                    :label="item.valueName"
                    :value="item.valueNum">
                  </el-option>
                </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="户型类别" prop="title"
                :rules="[
                 ]">
              <el-select v-model="inputForm.title" placeholder="请选择户型类别" size="small" style="width: 100%;">
                  <el-option
                    v-for="item in valueList" v-if="item.type==3"
                    :key="item.valueNum"
                    :label="item.valueName"
                    :value="item.valueName">
                  </el-option>
                </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="图片" prop="image"
              :rules="[
               ]">
            <el-upload ref="image"
            v-if="visible"
            list-type="picture-card"
                  :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/thouseinfo/tHouseType`"
                  :headers="{token: $cookie.get('token')}"
                  :on-preview="(file, fileList) => {
                      $alert(`<img style='width:100%' src=' ${(file.response && file.response.url) || file.url}'/>`,  {
                        dangerouslyUseHTMLString: true,
                        showConfirmButton: false,
                        closeOnClickModal: true,
                        customClass: 'showPic'
                      });
                  }"
                  :on-success="(response, file, fileList) => {
                     inputForm.image = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                  }"
                  :on-remove="(file, fileList) => {
                    $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                      $message.success(data.msg)
                    })
                    inputForm.image = fileList.map(item => item.url).join('|')
                  }"
                  :before-remove="(file, fileList) => {
                    return $confirm(`确定移除 ${file.name}？`)
                  }"
                  multiple
                  :limit="1"
                  :on-exceed="(files, fileList) =>{
                    $message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                  }"
                  :file-list="imageArra">
                  <i class="el-icon-plus"></i>
                </el-upload>
         </el-form-item>
      </el-col>
        <el-col :span="12">
            <el-form-item label="建筑面积" prop="floorage"
                :rules="[
                 ]">
              <el-input v-model="inputForm.floorage" placeholder="请填写建筑面积"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="价格" prop="price"
                :rules="[
                 ]">
              <el-input v-model="inputForm.price" placeholder="请填写价格"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="户型类型" prop="houseType"
                :rules="[
                 ]">
                 <el-select v-model="inputForm.houseType" placeholder="请选择户型类别" size="small" style="width: 100%;">
                  <el-option
                    v-for="item in valueList" v-if="item.type==17"
                    :key="item.valueNum"
                    :label="item.valueName"
                    :value="item.valueName">
                  </el-option>
                </el-select>
           </el-form-item>
        </el-col>
        <!-- <el-col :span="24">
            <el-form-item label="内容" prop="content"
                :rules="[
                 ]">
            <WangEditor ref="contentEditor" v-model="inputForm.content"/>
           </el-form-item>
        </el-col> -->
        <!-- <el-col :span="12">
            <el-form-item label="状态" prop="status"
                :rules="[
                 ]">
              <el-input v-model="inputForm.status" placeholder="请填写状态"     ></el-input>
           </el-form-item>
        </el-col> -->
        <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import GridSelect from '@/components/gridSelect'
  import WangEditor from '@/components/editor/WangEditor'
  export default {
    data () {
      return {
        valueList:[],
        title: '',
        method: '',
        visible: false,
        loading: false,
        imageArra:[],
        inputForm: {
          id: '',
          buildings: {
            id: ''
          },
          image:'',
          type: '',
          title: '',
          floorage: '',
          price: '',
          houseType: '',
          content: '',
          status: '',
          remarks: ''
        }
      }
    },
    components: {
      GridSelect,
      WangEditor
    },
    mounted () {
      this.getvalueList()
    },
    methods: {
      getvalueList () { 
        this.$http({
          url: '/valueinfo/tValueInfo/list',
          method: 'get',
          params: {
            'pageNo': 1,
            'pageSize': 999,
            'orderBy': this.orderBy,
            'type' : '16,17,3'
          }
        }).then(({data}) => {
          if (data && data.success) { 
            this.valueList = data.page.list 
          }
        })
      },
      init (method, id) {
        this.method = method
        if (method === 'add') {
          this.inputForm.buildings.id = id;
          this.title = `新建户型介绍`
        } else if (method === 'edit') {
          this.inputForm.id = id
          this.title = '修改户型介绍'
        } else if (method === 'view') {
          this.inputForm.id = id
          this.title = '查看户型介绍'
        }
        this.imageArra = []
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          // this.$refs.contentEditor.init('')
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/thouseinfo/tHouseType/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.tHouseType)
              this.inputForm.image.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.imageArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              // this.$refs.contentEditor.init(this.inputForm.content)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/thouseinfo/tHouseType/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>
<style lang="less" scoped>
  .showPic .el-message-box__message{
      width: 800px;
      height: 800px;
      overflow: hidden;
      display: flex;
       p img {
        height: 100%!important;
        width: 100%!important;
        object-fit: contain;
      }
    }
    </style>